import config from '@/config/config.js'
import { getToken ,setToken} from '@/utils/auth'
import tool from '@/utils/common.js';
let timeout = 10000
const baseUrl = config.baseUrl

const upload = config => {
    const isToken = config.headers.isToken;
    config.header = config.header || {};
	console.log(getToken());
    if (getToken() && isToken) {
        config.header['Authorization'] = uni.getStorageSync('token_type') + uni.getStorageSync('token'); 
		config.header['Content-Type'] = 'multipart/form-data';
		
    };
    if (config.params) {
        let url = config.url + '?' + tool.tansParams(config.params)
        url = url.slice(0, -1)
        config.url = url
    }
    return new Promise((resolve, reject) => {
        uni.uploadFile({
            timeout: config.timeout || timeout,
            url: baseUrl + config.url,
            filePath: config.filePath,
            name: config.name || 'file',
            header: config.header,
            formData: config.formData,
            success: (response) => {
                let result;
                // try {
                //     result = JSON.parse(response.data);
                // } catch (error) {
                //     tool.toastTip('响应数据不是有效的 JSON 格式', 'none', 1500);
                //     reject(error);
                //     return;
                // }
                // const code = result.code || 200
                // const msg = result.msg
				console.log('response: ',response);
				if(!getToken()){ setToken(res.header.accesstoken)}
				resolve(response) 
                // if (code === 200) {
                //     resolve(result)
                // } else if (code == 401) {
                //     tool.showConfirm('登录状态已过期，您可以继续留在该页面，或者重新登录?').then(res => {
                //         if (res.confirm) {
                //             uni.reLaunch({ url: '/pages/login' })
                //         }
                //     })
                //     reject('无效的会话，或者会话已过期，请重新登录。')
                // } else if (code === 500) {
                //     tool.toastTip(msg, 'none', 1500)
                //     reject('500')
                // } else if (code !== 200) {
                //     tool.toastTip(msg, 'none', 1500)
                //     reject(code)
                // }
            },
            fail: (error) => {
                let { message } = error;
                if (message === 'Network Error') {
                    message = '后端接口连接异常'
                } else if (message.includes('timeout')) {
                    message = '系统接口请求超时'
                } else if (message.includes('Request failed with status code')) {
                    message = '系统接口' + message.substr(message.length - 3) + '异常'
                }
                tool.toastTip(message, 'none', 1500)
                reject(error)
            }
        })
    })
}

export default upload